% Autor:
% Fecha: 16/06/2014

:- dynamic actualSituation/1, perception/3, posicionAgente/2, executedAction/2, visitada/1, tesoroEnCiudad/4, valorTesoroAgente/2, pesoTesoroAgente/2.


% definicion de ciudades adyacentes:

adyacente(ergintone,bulovoe).
adyacente(ergintone,peregaey).
adyacente(ergintone,aragaey).

adyacente(bulovoe,icaedur).
adyacente(bulovoe,ergintone).

adyacente(peregaey,ergintone).
adyacente(peregaey,vinizaile).

adyacente(aragaey,ergintone).
adyacente(aragaey,bresol).

adyacente(icaedur,vinizaile).
adyacente(icaedur,bulovoe).

adyacente(vinizaile,peregaey).
adyacente(vinizaile,icaedur).
adyacente(vinizaile,bresol).
adyacente(vinizaile,mixocu).
adyacente(vinizaile,cabe).

adyacente(bresol,aragaey).
adyacente(bresol,vinizaile).

adyacente(mixocu,vinizaile).
adyacente(mixocu,ase).

adyacente(cabe,vinizaile).

adyacente(ase,cenede).

adyacente(cenede,ase).

%% relacion ciudad-accion:



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                      REGLAS DE DIAGNOSTICO                              %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        % percepcion -> ciudadActual, pesoCiudadActual, valorCiudadActual
posicionAgente(X,S) :- perception(X,_,_),actualSituation(S).


tesoroEnCiudad(X,Y,Z,S) :- perception(X,Y,Z),actualSituation(S).


tesoroAgente(X,Y,S):- executedAction(tomarCofre,S), Y is Y+Y1, X is X+X1, perception(_,X1,Y1),actualSituation(S).
tesoroAgente(X,Y,S):- X is 0, Y is 0,actualSituation(S).

visitada(X) :- perception(X,_,_).




%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                       Axiomas de Estado Sucesor                         %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% no cambia la posicion:
est(S1) :- S1 > 0, S is S1-1, executedAction(tomarCofre,S),posicionAgente(X,S),tesoroAgente(X,P,V,S),tesoroEnCiudad(X,Pc,Vc,S),Pnuevo is P+Pc, Vnuevo is V+Vc,asserta(posicionAgente(X,S1)),asserta(tesoroEnCiudad(X,0,0,S1)),asserta(tesoroAgente(X,Pnuevo,Vnuevo,S)).

%% cambia la posicion:
est(S1) :- S1 > 0, S is S1-1, executedAction(irCenede,S),adyacente(X,cenede),posicionAgente(X,S),asserta(posicionAgente(cenede,S1)),asserta(visitada(cenede)).
est(S1) :- S1 > 0, S is S1-1, executedAction(irCabe,S),adyacente(X,cabe),posicionAgente(X,S),asserta(posicionAgente(cabe,S1)),asserta(visitada(cabe)).
est(S1) :- S1 > 0, S is S1-1, executedAction(irMixocu,S),adyacente(X,mixocu),posicionAgente(X,S),asserta(posicionAgente(mixocu,S1)),asserta(visitada(mixocu)).
est(S1) :- S1 > 0, S is S1-1, executedAction(irAse,S),adyacente(X,ase),posicionAgente(X,S),asserta(posicionAgente(ase,S1)),asserta(visitada(ase)).
est(S1) :- S1 > 0, S is S1-1, executedAction(irVinizaile,S),adyacente(X,vinizaile),posicionAgente(X,S),asserta(posicionAgente(vinizaile,S1)),asserta(visitada(vinizaile)).
%est(S1) :- S1 > 0, S is S1-1, executedAction(irBresol,S),adyacente(X,bresol),posicionAgente(X,S),tesoroAgente(X,P,V,S),tesoroEnCiudad(X,Pc,Vc,S),asserta(posicionAgente(X,S1)),asserta(tesoroEnCiudad(X,Pc,Vc,S1)),asserta(tesoroAgente(X,P,V,S)),asserta(posicionAgente(bresol,S1)),asserta(visitada(bresol)).
est(S1) :- S1 > 0, S is S1-1, executedAction(irBresol,S),adyacente(X,bresol),posicionAgente(X,S),asserta(posicionAgente(bresol,S1)),asserta(visitada(bresol)).
est(S1) :- S1 > 0, S is S1-1, executedAction(irAragaey,S),adyacente(X,aragaey),posicionAgente(X,S),asserta(posicionAgente(aragaey,S1)),asserta(visitada(aragey)).
est(S1) :- S1 > 0, S is S1-1, executedAction(irPeregaey,S),adyacente(X,peregaey),posicionAgente(X,S),asserta(posicionAgente(peregaey,S1)),asserta(visitada(peregaey)).
est(S1) :- S1 > 0, S is S1-1, executedAction(irErgintone,S),adyacente(X,ergintone),posicionAgente(X,S),asserta(posicionAgente(ergintone,S1)),asserta(visitada(ergintone)).
est(S1) :- S1 > 0, S is S1-1, executedAction(irIcaedur,S),adyacente(X,icaedur),posicionAgente(X,S),asserta(posicionAgente(icaedur,S1)),asserta(visitada(icaedur)).
est(S1) :- S1 > 0, S is S1-1, executedAction(irBulovoe,S),adyacente(X,bulovoe),posicionAgente(X,S),asserta(posicionAgente(bulovoe,S1)),asserta(visitada(bulovoe)).





excelent(tomarCofre,S) :- tesoroAgente(Pa,_,S), tesoroEnCiudad(X,Pc,Vc,S),Vc>0,posicionAgente(X,S),Ptotal is Pa+Pc,Ptotal<450.

very_good(irCenede,S) :- actualSituation(S),adyacente(X,cenede),posicionAgente(X,S).


good(irMixocu,S) :- actualSituation(S),adyacente(X,mixocu),posicionAgente(X,S), not(visitada(mixocu)).
good(irAse,S) :- actualSituation(S),adyacente(X,ase),posicionAgente(X,S), not(visitada(ase)).
good(irCabe,S) :- actualSituation(S),adyacente(X,cabe),posicionAgente(X,S), not(visitada(cabe)).
good(irVinizaile,S) :- actualSituation(S),adyacente(X,vinizaile),posicionAgente(X,S), not(visitada(vinizaile)).
good(irBresol,S) :- actualSituation(S),adyacente(X,bresol),posicionAgente(X,S), not(visitada(bresol)).
good(irAragaey,S) :- actualSituation(S),adyacente(X,aragaey),posicionAgente(X,S), not(visitada(aragaey)).
good(irPeregaey,S) :- actualSituation(S),adyacente(X,peregaey),posicionAgente(X,S), not(visitada(peregaey)).
good(irErgintone,S) :- actualSituation(S),adyacente(X,ergintone),posicionAgente(X,S), not(visitada(ergintone)).
good(irIcaedur,S) :- actualSituation(S),adyacente(X,icaedur),posicionAgente(X,S), not(visitada(icaedur)).
good(irBulovoe,S) :- actualSituation(S),adyacente(X,bulovoe),posicionAgente(X,S), not(visitada(bulovoe)).


bestAction(noAction,S) :- goalReached(S),!.
bestAction(X,S) :- excelent(X,S),!.
bestAction(X,S) :- very_good(X,S),!.
bestAction(X,S) :- good(X,S),!.


goalReached(S) :- posicionAgente(cenede,S).

